/** @format */

const { Controller } = require('egg');
const fs = require('fs');
const path = require('path');
class FilesController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = 'hi, egg';
    }
    async imageupload() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        let files = ctx.request.files;
        // file.forEach((item, index) => {
        //     console.log(item, index);
        // });
        console.log(files, 'sss');
        console.log(clientdata, 'clientdata');
        if (files.length != 0) {
            let filesurl = [];
            try {
                files.forEach((item, index) => {
                    let newpath = path.dirname(__dirname) + `/public/images/${path.basename(item.filepath)}`;
                    fs.copyFileSync(item.filepath, newpath);
                    const images = `http://127.0.0.1:8000/images/${path.basename(item.filepath)}`;
                    filesurl.push(images);
                });
            } catch (error) {
                console.log(error);
            }

            ctx.body = {
                code: 200,
                imagerul: filesurl,
                desc: '文件上传成功',
            };
        } else {
            ctx.body = {
                code: 210,
                desc: '文件上传失败，请重新上传',
            };
        }
    }
    async uploadImage() {
        const { ctx, app } = this;
        let clientdata = ctx.request.body;
        let files = ctx.request.files;
        try {
            let data = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(data, '我是用户');
        } catch (error) {
            console.error(error);
        }
        if (files.length != 0) {
            let filesurl = [];
            try {
                files.forEach((item, index) => {
                    let newpath = path.dirname(__dirname) + `/public/images/${path.basename(item.filepath)}`;
                    fs.copyFileSync(item.filepath, newpath);
                    // const images = `http://120.26.56.51:7001/images/${path.basename(item.filepath)}`;
                    const images = `http://127.0.0.1:7001/images/${path.basename(item.filepath)}`;

                    filesurl.push(images);
                });
            } catch (error) {
                console.log(error);
            }

            ctx.body = {
                code: 200,
                imagerul: filesurl,
                desc: '文件上传成功',
            };
        } else {
            ctx.body = {
                code: 210,
                desc: '文件上传失败，请重新上传',
            };
        }
    }
    async publish() {
        const { ctx, app } = this;
        let clientdata = ctx.request.body;
        try {
            let data = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(data, '我是用户');
            clientdata.imageUrl.forEach(async (item, index) => {
                await this.app.mysql.insert('images', {
                    username: data.username,
                    image: item,
                    imagedesp: clientdata.imagedesp,
                });
            });
            ctx.body = {
                code: 200,
                desc: '发布成功',
            };
        } catch (error) {
            console.error(error);
        }
    }
    async getpicture() {
        const { ctx, app } = this;

        try {
            let data = app.jwt.verify(ctx.request.header.token, app.config.jwt.secret);
            console.log(data, '我是用户');

            const results = await this.app.mysql.select('images', {
                // 搜索 posts 表
                where: { username: data.username }, // WHERE 条件
            });
            ctx.body = {
                code: 200,
                data: results,
                desc: '获取成功',
            };
        } catch (error) {
            console.error(error);
        }
    }
    async imageselect() {
        const { ctx } = this;
        let clientdata = ctx.request.body;
        let file = ctx.request.files;
        console.log(clientdata, 'clientdata', file);
        ctx.body = {
            code: 200,
            desc: '文件上传成功',
        };
    }
}

module.exports = FilesController;
